<template>
  <div class="login-wrap">
    <div class="login-area">
      <el-form ref="ruleFormRef" :model="searchForm">
        <h2 class="-enter-x">登录</h2>
        <div class="title -enter-x">动态权限示例平台</div>
        <el-form-item class="-enter-x" prop="account" :rules="isNoEmpty">
          <el-input
            v-model="searchForm.account"
            :prefix-icon="UserFilled"
            placeholder="请输入账号"
          />
        </el-form-item>
        <el-form-item class="-enter-x" prop="password" :rules="isNoEmpty">
          <el-input
            v-model="searchForm.password"
            placeholder="请输入密码"
            :prefix-icon="Unlock"
            :type="!showPassword ? 'password' : 'text'"
          >
            <template #suffix>
              <el-icon style="cursor: pointer" @click="showPassword = !showPassword">
                <Hide v-show="showPassword" />
                <View v-show="!showPassword" />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item class="-enter-x">
          <button type="button" class="base-button" @click="handleAccountLogin(ruleFormRef)">
            登录
          </button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts" name="LoginPage">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'
import { UserFilled, Unlock, View, Hide } from '@element-plus/icons-vue'
import { wrapperToken } from '@/utils'
import { isNoEmpty } from '@/utils/rules'
import { useUserStore } from '@/store/modules/user'
import { doLoginApi } from '@/api/login'

const router = useRouter()
const userStore = useUserStore()

const ruleFormRef = ref<FormInstance>()

// 用于实现：在password没有值时也能切换是否展示密码功能
const showPassword = ref(false)
const searchForm = ref({
  account: 'admin',
  password: 'admin'
})

// 账号密码登录
const handleAccountLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) {
    return
  }
  try {
    await formEl.validate(async valid => {
      if (valid) {
        const { code, data } = await doLoginApi(searchForm.value)
        if (code === 0) {
          userStore.setUserInfo(data)
          wrapperToken.setToken(data.access_token)
          router.push('/')
        }
      }
    })
  } catch (err) {
    console.log(err)
  }
}
</script>

<style lang="scss" scoped>
.login-wrap {
  width: 100%;
  height: 100%;
  background: url('/src/assets/img/login/bg.png');
  background-size: 100% 100%;
  position: relative;

  :deep(.login-area) {
    width: 738px;
    height: 100%;
    background: #fff;
    border-radius: 10px 0 0 10px;
    position: absolute;
    top: 0;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;

    h2 {
      font-weight: bold;
      font-size: 40px;
    }

    .title {
      font-size: 24px;
      color: #999999;
      margin: 20px 0 78px;
    }

    .el-form {
      width: 448px;

      .el-form-item {
        margin-bottom: 20px;

        .el-input__wrapper {
          width: 100%;
          height: 56px;
          border-radius: 6px;
          background: #eeeeee;
          box-shadow: none;
        }

        .base-button {
          margin-top: 20px;
          width: 447px;
          height: 56px;
          line-height: 56px;
          font-size: 20px;
          font-weight: 500;
        }
      }
    }
  }
}
</style>
